import React, { Component } from "react";
import { Button, DatePicker } from "antd";
import MsgManagerBuilder from "./messageManager";

export default class Test3 extends Component {
  state = {
    name: 1111,
    msgMan: null,
    msgMan2: null,
  };

  url = "http://localhost:8080/api";
  url2 = "http://localhost:8080/test2";

  postMessage = (count) => {
    const { msgMan } = this.state;
    for (let i = 0; i < count; i++) {
      msgMan.push({ id: i });
    }
  };

  action = (postingMsgList, resolve, reject) => {
    return fetch(this.url, {
      method: "post",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        data: postingMsgList,
      }),
    }).then((response) => {
      // const random = Math.random()
      // if(random > 0.5) {
      resolve(response);
      // } else {
      //   reject(response)
      // }
    });
  };

  action2 = (postingMsgList, resolve, reject) => {
    return fetch(this.url2, {
      method: "post",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        data: postingMsgList,
      }),
    }).then((response) => {
      // const random = Math.random()
      // if(random > 0.5) {
      resolve(response);
      // } else {
      //   reject(response)
      // }
    });
  };

  componentDidMount = () => {
    const config = {
      action: this.action,
      interval: 5000,
      onceHandleLength: 3,
      callback: () => {
        console.log("请求成功！！");
      },
      log: (logContent) => {
        const { errMsg, hasSaveCount, waitingForSaveCount, currentCount } =
          logContent;
        if (errMsg) {
          console.log(`等待传出数量：${waitingForSaveCount},  异常：${errMsg}`);
        } else {
          console.log(
            `本次传出数量${currentCount}, 累计传输数量：${hasSaveCount}, 等待传出数量：${waitingForSaveCount}`
          );
        }
      },
      errorHandle: (error, postingMsgList, waitPostMessageList) => {
        console.log(error);
      },
    };
    const msgMan = MsgManagerBuilder.init(config);

    const config2 = {
      action: this.action2,
      interval: 5000,
      onceHandleLength: 3,
      callback: () => {
        console.log("请求成功！！");
      },
      log: (logContent) => {
        const { errMsg, hasSaveCount, waitingForSaveCount, currentCount } =
          logContent;
        if (errMsg) {
          console.log(`等待传出数量：${waitingForSaveCount},  异常：${errMsg}`);
        } else {
          console.log(
            `本次传出数量${currentCount}, 累计传输数量：${hasSaveCount}, 等待传出数量：${waitingForSaveCount}`
          );
        }
      },
      errorHandle: (error, postingMsgList, waitPostMessageList) => {
        console.log(error);
      },
    };
    const msgMan2 = MsgManagerBuilder.init(config2);

    this.setState({
      msgMan,
      msgMan2,
    });
  };

  postMessage2 = (count) => {
    const { msgMan2 } = this.state;
    for (let i = 0; i < count; i++) {
      msgMan2.push({ id: i });
    }
  };

  render() {
    return (
      <>
        <Button onClick={() => this.postMessage(1)}>发送1条</Button>

        <Button onClick={() => this.postMessage(2)}>发送2条</Button>

        <Button onClick={() => this.postMessage(3)}>发送3条</Button>

        <Button onClick={() => this.postMessage(5)}>发送5条</Button>

        <Button onClick={() => this.postMessage(10)}>发送10条</Button>
        <br /> <br /> <br /> <br />

        <Button onClick={() => this.postMessage2(1)}>发送1条</Button>

        <Button onClick={() => this.postMessage2(2)}>发送2条</Button>

        <Button onClick={() => this.postMessage2(3)}>发送3条</Button>

        <Button onClick={() => this.postMessage2(5)}>发送5条</Button>

        <Button onClick={() => this.postMessage2(10)}>发送10条</Button>
      </>
    );
  }
}

// 3   6
// 4

// 6  4
// 3
